package ro.gateway.aida.fnd;

import java.util.ArrayList;

import ro.gateway.aida.admin.PropertyListItem;
import ro.gateway.aida.obj.AIDAActivityObject;
import ro.gateway.aida.obj.Organization;


/**
 * <p>Title: Romanian AIDA</p> <p>Description: :D application</p> <p>Copyright: Copyright (comparator) 2003</p>
 * <p>Company: Romania Development Gateway </p>
 *
 * @author Mihai Popoaei, mihai_popoaei@yahoo.com, smike@intellisource.ro
 * @version 1.0-* @version $Id: FundingItem.java,v 1.1 2004/10/24 23:37:13 mihaipostelnicu Exp $
 */
public class FundingItem extends AIDAActivityObject {
	private long org_id;
	private Organization org;
	private long type_id = -1;
//     private FundingType type; -> terms of assistance
	private long status_id = -1;
	private PropertyListItem status;
	private PropertyListItem type_of_assistance;
	private PropertyListItem terms_of_assistance;
	private String currency;
	private double amount = 0;
	private long signature_date = -1;
	/**
	 * @deprecated
	 */
	private ArrayList yearlyItems;
	/**
	 * @deprecated
	 */
	private ArrayList byCatItems;
	/**
	 * @deprecated
	 */
	private ArrayList persons;
	private ArrayList disbursements;
	private ArrayList commitments;
	private String comments;

	public PropertyListItem getType_of_assistance() {
		return type_of_assistance;
	}

	public void setType_of_assistance(PropertyListItem type_of_assistance) {
		this.type_of_assistance = type_of_assistance;
	}

	public PropertyListItem getTerms_of_assistance() {
		return terms_of_assistance;
	}

	public void setTerms_of_assistance(PropertyListItem terms_of_assistance) {
		this.terms_of_assistance = terms_of_assistance;
	}

	public long getSignature_date() {
		return signature_date;
	}

	public void setSignature_date(long signature_date) {
		this.signature_date = signature_date;
	}


	public String getComments() {
		return comments;
	}

	public void setComments(String comment) {
		this.comments = comment;
	}

	public long getStatus_id() {
		return status_id;
	}

	public long getType_id() {
		return type_id;
	}


	public FundingItem() {
		this.yearlyItems = new ArrayList();
		this.byCatItems = new ArrayList();
		this.objectType = AIDAActivityObject.TYPE_FUNDING;
		this.persons = new ArrayList();
		this.disbursements = new ArrayList();
		this.commitments = new ArrayList();
		this.currency = "USD";
	}

	public ArrayList getDisbursements() {
		return disbursements;
	}

	public void setDisbursements(ArrayList disbursements) {
		this.disbursements = disbursements;
	}

	public ArrayList getCommitments() {
		return commitments;
	}

	public void setCommitments(ArrayList commitments) {
		this.commitments = commitments;
	}

	public ArrayList getPersons() {
		return persons;
	}

	public void setPersons(ArrayList persons) {
		this.persons = persons;
	}

	public void addYearlyItem(FundingYearlyItem item) {
		if (!this.containsYearlyItem(item)) {
			this.yearlyItems.add(item);
		}
	}

	public boolean containsYearlyItem(FundingYearlyItem item) {
		for (int i = 0; i < yearlyItems.size(); i++) {
			FundingYearlyItem fundingYearlyItem = (FundingYearlyItem) yearlyItems.get(i);
			if (fundingYearlyItem.equals(item))
				return true;
		}

		return false;
	}

	public FundingYearlyItem[] getYearlyItemsArray() {
		if (yearlyItems.size() > 0) {
			FundingYearlyItem[] result = new FundingYearlyItem[yearlyItems.size()];
			yearlyItems.toArray(result);
			return result;
		} else {
			return null;
		}
	}

	public ArrayList getYearlyItems() {
		return yearlyItems;
	}

	public void removeYearlyItem(int index) {
		if ((index > 0) && (index < yearlyItems.size())) {
			yearlyItems.remove(index);
		}
	}

	public void addByCatItem(FundingByCatItem item) {
		if (!this.containsByCatItem(item)) {
			this.byCatItems.add(item);
		}
	}

	public boolean containsByCatItem(FundingByCatItem item) {
		for (int i = 0; i < byCatItems.size(); i++) {
			FundingByCatItem fundingByCatItem = (FundingByCatItem) byCatItems.get(i);
			if (fundingByCatItem.equals(item))
				return true;
		}

		return false;
	}

	public ArrayList getByCatItems() {
		return byCatItems;
	}

	public FundingByCatItem[] getByCatItemsArray() {
		if (byCatItems.size() > 0) {
			FundingByCatItem[] result = new FundingByCatItem[byCatItems.size()];
			byCatItems.toArray(result);
			return result;
		} else {
			return null;
		}
	}

	public void removeByCatItem(int index) {
		if ((index > 0) && (index < byCatItems.size())) {
			byCatItems.remove(index);
		}
	}

	public void setAmount(double amount) {
		this.amount = amount;
	}

	public void setCurrency(String currency) {
		this.currency = currency;
	}

	public void setStatus(PropertyListItem status) {
		this.status = status;
		if (status != null) {
			this.status_id = status.getId();
		} else {
			this.status_id = -1;
		}
	}

//     public void setType( FundingType type ) {
//          this.type = type;
//          if ( type != null ) {
//               this.type_id = type.getId();
//          } else {
//               this.type_id = -1;
//          }
//     }
//
//     public void setType_id( long type_id ) {
//          this.type_id = type_id;
//     }

	public void setStatus_id(long status_id) {
		this.status_id = status_id;
	}

	public void setOrg(Organization org) {
		this.org = org;
		if (org != null) {
			this.org_id = org.getId();
		} else {
			this.org_id = -1;
		}
	}

	public void setOrg_id(long org_id) {
		this.org_id = org_id;
	}

	public double getAmount() {
		return amount;
	}

	public String getCurrency() {
		return currency;
	}

	public PropertyListItem getStatus() {
		return status;
	}

//     public FundingType getType() {
//          return type;
//     }

	public Organization getOrg() {
		return org;
	}

	public long getOrg_id() {
		return org_id;
	}

	public boolean isValid() {
		return (org != null)
			 && (status != null)
			 && (currency != null)
			 && (amount > 0);
	}

	public void setId(long id) {
		super.setId(id);
		for (int i = 0; i < yearlyItems.size(); i++) {
			FundingYearlyItem fundingYearlyItem = (FundingYearlyItem) yearlyItems.get(i);
			fundingYearlyItem.setFunding_id(id);
		}

		for (int i = 0; i < byCatItems.size(); i++) {
			FundingByCatItem byCatItem = (FundingByCatItem) byCatItems.get(i);
			byCatItem.setFunding_id(id);
		}
	}

	public Object clone() {
		FundingItem result = new FundingItem();
		result.populate(this);
		return result;
	}

	public void populate(FundingItem item) {
		super.populate(item);

		FundingItem result = this;
		result.setId(item.id);
		result.setOrg_id(item.org_id);
		result.setOrg(item.org);
		result.setAmount(item.amount);
		result.setActivityId(item.activityId);
		result.setCurrency(item.currency);
		result.setPersons(item.persons);
		result.setStatus_id(item.status_id);
		result.setStatus(item.status);
//          result.setType( item.type );
//          result.setType_id( item.type_id );
		result.setUser_id(item.user_id);
		result.byCatItems = (ArrayList) item.byCatItems.clone();
		result.yearlyItems = (ArrayList) item.yearlyItems.clone();
		result.setComments(item.getComments());
		result.setType_of_assistance(item.getType_of_assistance());
		result.setTerms_of_assistance(item.getTerms_of_assistance());
		result.setCommitments(item.getCommitments());
		result.setDisbursements(item.getDisbursements());
		result.setSignature_date(item.getSignature_date());
	}
}
